package com.shntec.temp.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.shntec.bp.json.JSONArray;
import com.shntec.bp.json.JSONException;
import com.shntec.bp.json.JSONObject;

import com.shntec.bp.common.ShntecBaseAction;
import com.shntec.bp.common.ShntecErrorCode;
import com.shntec.bp.common.JSONMessageConstant;
import com.shntec.bp.common.RequestMessageParser;
import com.shntec.bp.common.ResponseMessageGenerator;
import com.shntec.bp.util.DatabaseManager;
import com.shntec.bp.util.ShntecLogger;
import com.shntec.temp.util.MySQLConn;

public class GetDateListAction extends ShntecBaseAction {

	public GetDateListAction () {
		super.actionCode = 20002;
		super.actionName = "GetDateListAction";
		super.actionDescription = "获取日期列表";
		// No need authentication
		super.needAuthenticationCheck = false;
	}

	@Override
	public ResponseMessageGenerator handleAction(RequestMessageParser parser) {

		ResponseMessageGenerator generator = new ResponseMessageGenerator();

		JSONObject payload = null;
		JSONObject parameter = parser.getParameterJsonObject();
		
		
		if (parameter == null) {

			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER);
		}
		
		
		int index = -1;
		int count = -1;
		int uid = -1;
		
		try {
			index = parameter.getInt("index");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"index\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"index\"");
		}
		
		try {
			count = parameter.getInt("count");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"count\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"count\"");
		}
		
		try {
			uid = parameter.getInt("uid");
		} catch (JSONException e) {
			ShntecLogger.logger.error("Missing required parameter: \"uid\"" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"缺少所需的参数: \"uid\"");
		}
		
		MySQLConn mysql = new MySQLConn();
		Connection conn = null;
		payload = new JSONObject();
		JSONArray datearray = new JSONArray();
		int rowtotal = 0;
		try {
			payload.put("date", datearray);
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = mysql.getConn();
			pstmt = conn.prepareStatement("SELECT d.did,d.description,d.datevalue,m.mid,m.TodayPlans,m.Completion,m.question FROM temp_date d,temp_meeting m WHERE m.uid = ? AND d.did = m.did ORDER BY datevalue DESC LIMIT ? OFFSET ?");
			pstmt.setInt(1, uid);
			pstmt.setInt(2, count);
			pstmt.setInt(3, index);
			rs = pstmt.executeQuery();
			
			while(rs.next()){
				
				JSONObject item = new JSONObject();
				item.put("did", rs.getInt("did"));
				item.put("datevalue", rs.getDate("datevalue"));
				item.put("description", rs.getString("description"));
				item.put("mid", rs.getInt("mid"));
				
				String todayPlans = rs.getString("todayPlans");
				String Completion = rs.getString("Completion");
				String question = rs.getString("question");
				boolean today = false;
				boolean com = false;
				boolean questionboolean = false;
				
				if(todayPlans == null || todayPlans.equals("")){
					
				}else{
					today = true;
				}
				
				if(Completion == null || Completion.equals("")){
					
				}else{
					com = true;
				}
				if(question == null || question.equals("")){
					
				}else{
					questionboolean = true;
				}
				
				item.put("today", today);
				item.put("com", com);
				item.put("question", questionboolean);
				
				datearray.put(item);
				rowtotal ++;
			}
			
			payload.put("rowtotal", rowtotal);
			
		
			return generator.toSuccess(parser, payload);
			

			
		} catch (ClassNotFoundException e) {
			ShntecLogger.logger.error("ClassNotFoundException" );
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"ClassNotFoundException");
		} catch (SQLException e) {
			ShntecLogger.logger.error("SQLException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"SQLException");
		} catch (JSONException e) {
			ShntecLogger.logger.error("JSONException");
			ShntecLogger.logger.error(e.getMessage());
			return generator.toError(parser, 
					ShntecErrorCode.SHNTEC_ERROR_CODE_MISSING_PARAMETER, 
					"JSONException");
		}finally{
			if(conn != null){
				
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			if(pstmt != null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
			if(rs != null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}

}
